<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\BlackWords;
use App\Util\InfoUtil;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;

class BlackWordsController extends Controller
{
    use ModelForm;

    public function index()
    {
        $content = new Content();
        $content->header('敏感词管理');
        $content->body($this->grid());
        return $content;
    }

    public function edit($id)
    {
        $content = new Content();
        $content->header('敏感词编辑');
        $content->body($this->form()->edit($id));
        return $content;
    }

    public function create()
    {
        $content = new Content();
        $content->header('敏感词添加');
        $content->body($this->form());
        return $content;
    }

    protected function grid()
    {
        $grid = new Grid(new BlackWords());
        $grid->model()->orderBy('id', 'desc');
        $grid->id('记录ID');
        $grid->word('敏感词')->editable();
        $grid->word_type('分类')->display(function ($data) {
            return BlackWords::TYPE[$data];
        });
        $grid->flag('状态')->display(function ($data) {
            return $data == 1 ? '有效' : '无效';
        });
        $grid->created_at('添加时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        //禁用导出
        $grid->disableExport();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 3, function ($filter) {
                $filter->like('word', '敏感词');
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('word_type', '分类')->select(BlackWords::TYPE);
            });
        });
        //禁用行选择
        $grid->disableRowSelector();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            // $actions->disableDelete();
            $actions->disableView();
        });
        return $grid;
    }

    protected function form()
    {
        $form = new Form(new BlackWords());
        $form->textarea('word', '敏感词')->rows(10);
        $form->select('word_type', '分类')->options(BlackWords::TYPE)->default(1)->required();
        $form->select('flag', '是否有效')->options([1 => '有效', 0 => '无效'])->default(1)->required();
        $form->saving(function (Form $form) {
            $words = explode("\r\n", $form->word);
            $items = [];
            $repeatWords = [];
            foreach ($words as $word) {
                if (BlackWords::where('word', $word)->count()) {
                    $repeatWords[] = $word;
                    continue;
                }
                $items[] = ['word' => $word, 'word_type' => $form->word_type];
            }
            $items && BlackWords::insert($items);
            if ($repeatWords) return InfoUtil::adminErr('库内重复敏感词：' . implode('，', $repeatWords));
            return InfoUtil::adminSuc('插入成功');
        });
        $form->saved(function (Form $form) {
        });
        return $form;
    }
}
